package com.example.file.service.impl;


import com.example.file.exception.ServiceException;
import com.example.file.model.LoginBody;
import com.example.file.security.User;
import com.example.file.security.utils.JwtUtils;
import com.example.file.service.LoginService;
import jakarta.annotation.PostConstruct;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import static com.example.file.response.ResultEnum.*;

@Service
public class LoginServiceImpl implements LoginService {

    @PostConstruct
    public void init() {
        // 获取当前工作目录
        String currentDirectory = System.getProperty("user.dir");
        System.out.println("currentDirectory = " + currentDirectory);
    }

    private static final String currentDirectory = System.getProperty("user.dir");
    private static final String USER_INFO_TXT_FILE_NAME = "user_info.txt";

    private static final String USER_INFO_TXT_PATH = currentDirectory + File.separator + USER_INFO_TXT_FILE_NAME;

    @Override
    public String login(LoginBody loginBody) {
        String username = loginBody.getUsername();
        String password = loginBody.getPassword();

        User user = getUser();
        if (!StringUtils.equals(username, user.getUsername()) || !StringUtils.equals(password, user.getPassword())) {
            throw new ServiceException(USERNAME_OR_PASSWORD_FAIL);
        }

        HashMap<String, String> stringStringHashMap = new HashMap<>();
        stringStringHashMap.put(username, password);
        return JwtUtils.getToken(stringStringHashMap);


    }

    @Override
    public void validUserExist() {
        User user = null;
        try {
            user = getUser();
        } catch (Exception e) {
//            throw new RuntimeException(e);
        }
        if (user != null) {
            throw new ServiceException(DISABLE_REGISTRATION);
        }
    }

    @Override
    public synchronized void registUser(LoginBody loginBody) {
        validUserExist();
        try {
            Files.write(
                    Paths.get(USER_INFO_TXT_PATH),
                    Arrays.asList(loginBody.getUsername(), loginBody.getPassword()),
                    StandardOpenOption.CREATE
            );
        } catch (IOException e) {
            throw new ServiceException(REGISTRATION_FAIL);
        }
    }

    private User getUser() {
        try {
            List<String> userInfos = Files.readAllLines(Paths.get(USER_INFO_TXT_PATH));
            if (CollectionUtils.isEmpty(userInfos) || userInfos.size() < 2) {
                throw new ServiceException(ACCOUNT_NOT_REGISTER);
            }
            return new User().setUsername(userInfos.get(0)).setPassword(userInfos.get(1));
        } catch (IOException e) {
            throw new ServiceException(ACCOUNT_NOT_REGISTER);
        }
    }
}
